package com.aiyige.utils;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;

/**
 * Created by usera on 2017/8/8.
 */

public class ImageViewResSwitcher {
    public static void switchEnter(final ImageView imageView, final int fromRes, final int toRes) {
        ObjectAnimator outScaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 1.0f, 0.5f);
        ObjectAnimator outScaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 1.0f, 0.5f);
        ObjectAnimator outAlpha = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f, 0.5f);
        AnimatorSet outSet = new AnimatorSet();
        outSet.play(outScaleX).with(outScaleY).with(outAlpha);
        outSet.setDuration(100);

        ObjectAnimator inScaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 0.5f, 1f);
        ObjectAnimator inScaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 0.5f, 1f);
        final ObjectAnimator inAlpha = ObjectAnimator.ofFloat(imageView, "alpha", 0.5f, 1f);
        final AnimatorSet inSet = new AnimatorSet();
        inSet.play(inScaleX).with(inScaleY).with(inAlpha);
        inSet.setInterpolator(new OvershootInterpolator(4));
        inSet.setDuration(100);
        inSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                imageView.setImageResource(toRes);
            }

            @Override
            public void onAnimationEnd(Animator animator) {

            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });


        outSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                imageView.setImageResource(fromRes);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                inSet.start();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        outSet.start();
    }

    public static void switchExit(final ImageView imageView, final int fromRes, final int toRes) {
        ObjectAnimator outScaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 1.0f, 0.5f);
        ObjectAnimator outScaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 1.0f, 0.5f);
        ObjectAnimator outAlpha = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f, 0.5f);
        AnimatorSet outSet = new AnimatorSet();
        outSet.play(outScaleX).with(outScaleY).with(outAlpha);
        outSet.setDuration(60);

        ObjectAnimator inScaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 0.5f, 1f);
        ObjectAnimator inScaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 0.5f, 1f);
        final ObjectAnimator inAlpha = ObjectAnimator.ofFloat(imageView, "alpha", 0.5f, 1f);
        final AnimatorSet inSet = new AnimatorSet();
        inSet.play(inScaleX).with(inScaleY).with(inAlpha);
        inSet.setDuration(60);
        inSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                imageView.setImageResource(toRes);
            }

            @Override
            public void onAnimationEnd(Animator animator) {

            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });


        outSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {
                imageView.setImageResource(fromRes);
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                inSet.start();
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });
        outSet.start();
    }

}
